<?php
/**
 * Description: 获取订单状态
 * User: devforma
 * Date: 15/6/2
 * Time: 12:39
 */
class GetOrderStatus implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $cmsdb;

        if ( empty($queryArgs['order_num']) || !is_numeric($queryArgs['order_num'])) {
            return genOutput(1000);
        }

        $sql = "SELECT `order_id`, `user_id`, `price`, `product_id`, `product_type`, `success`, `invalid_time` FROM `original_orders`
        WHERE `order_id`=:order_num";
        $record = $cmsdb->getRow($sql, array(':order_num' => $queryArgs['order_num']));

        if (empty($record)) //没有订单记录
            return genOutput(1000);

        //获取商品名称
        switch ($record->product_type) {
            case 'zhiboke':
                $zhiboke = $cmsdb->getRow('SELECT `course_name`, `need_address`, `valid_order_period`, `valid_pay_period`
                FROM `data_live_course` WHERE `id`=:cid', array(':cid' => $record->product_id));
                break;

            default:
        }

        return genOutput(1, array(
            'order' => array(
                'success' => $record->success == 1,
                'order_num' => $record->order_id,
                'user_id' => intval($record->user_id),
                'product_id' => intval($record->product_id),
                'price' => floatval($record->price),
                'product_name' => $zhiboke->course_name,
                'need_address' => $zhiboke->need_address == '1',
                'invalid_time' => empty($record->invalid_time) ? '' : $record->invalid_time,
                'valid_order_period' => empty($zhiboke->valid_order_period) ? 0 : $zhiboke->valid_order_period * 60,
                'valid_pay_period' => empty($zhiboke->valid_pay_period) ? 0 : $zhiboke->valid_pay_period * 60
            )
        )); //支付完成
    }

}
